package com.firefighting.app;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

/**
 * Servlet implementation class ChangePassword
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/ChangePassword" })
public class ChangePassword extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		

		String userId=request.getParameter("userId");
		String old_password=request.getParameter("old_password");
		String new_password=request.getParameter("new_password");

		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		//ResultSetMetaData data =null;
		try {
			connection = JdbcUtils.getConnection();
			String sql = "update  users set users.password=? where users.userId=?";
			ps = connection.prepareStatement(sql);
			ps.setString(1, new_password);
			ps.setString(2, userId);
			//ps.setString(3, old_password);
			 ps.executeUpdate();
			// data=rs.getMetaData();
			//System.out.print(data.getColumnCount());
			// System.out.print(data.getColumnName(1));
			
			if ( ps.executeUpdate()>0) {
				result_jb.put("status", "true");
				result_jb.put("message", "修改成功");
				//result_jb.put("password", rs.getString("password"));
				System.out.println(result_jb.toString());
			}else {
				result_jb.put("status", "false");
				result_jb.put("message", "修改失败");
				System.out.println("修改密码失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "修改失败");
		} finally {
		//	JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
